package com.mall507.fivepeaks.activity.main;

import android.os.Build;
import android.support.annotation.RequiresApi;
import android.text.Html;
import android.webkit.WebSettings;
import android.widget.TextView;

import com.alibaba.android.arouter.facade.annotation.Route;
import com.hzh.frame.comn.annotation.ViewInject;
import com.hzh.frame.comn.callback.HttpCallBack;
import com.hzh.frame.core.HttpFrame.BaseHttp;
import com.hzh.frame.ui.activity.BaseUI;
import com.hzh.frame.widget.xwebview.XEmbedWebView;
import com.mall507.fivepeaks.R;
import com.mall507.fivepeaks.widget.imageview.ExpandImageView;

import org.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject;

import java.util.ArrayList;

@Route(path = "/main/MainTownShipDongTaiInfoUI")
public class MainTownShipDongTaiInfoUI extends BaseUI {
    @ViewInject(R.id.tv_title)
    TextView title;
    @ViewInject(R.id.views)
    TextView views;
    @ViewInject(R.id.reporter)
    TextView reporter;
    @ViewInject(R.id.time)
    TextView time;
    @ViewInject(R.id.icon)
    ExpandImageView icon;
    @ViewInject(R.id.content)
    XEmbedWebView content;
    
    @Override
    protected void onCreateBase() {
        setContentView(R.layout.ui_main_township_dongtai_info);
        getTitleView().setContent("乡镇动态");
        showLoding();
        getComment();
        content.init();
        if (Build.VERSION.SDK_INT > Build.VERSION_CODES.LOLLIPOP) {
            content.getSettings().setMixedContentMode(WebSettings.MIXED_CONTENT_ALWAYS_ALLOW);
        }
    }

    private void getComment() {
        JSONObject params = new JSONObject();
        try {
            params.put("id", getIntent().getStringExtra("id"));
        } catch (JSONException e) {
            e.printStackTrace();
        }
        BaseHttp.getInstance().query("Township/getTtownshipDynamicOne", params, new HttpCallBack() {
            @RequiresApi(api = Build.VERSION_CODES.N)
            @Override
            public void onSuccess(JSONObject response) {
                if (200 == response.optInt("code")){
                    JSONObject data = response.optJSONObject("data");
                    title.setText(data.optString("title"));
                    views.setText("访问次数：" + data.optString("dynamic_browse_num"));
                    reporter.setText("记者：" + data.optString("author"));
                    time.setText("发布时间：" + data.optString("create_time"));

                    content.loadDataWithBaseURL(
                            null, 
                            getHtmlData(data.optString("desc")),
                            "text/html", 
                            "utf-8", 
                            null);
                    //content.loadUrl("file:////android_asset/333.html");
                }
                dismissLoding();
            }
        });
    }

    private String getHtmlData(String bodyHTML) {
        String head = "<head><style>img{width: 100%;}</style></head>";
        return "<html>" + head + "<body>" + bodyHTML + "</body></html>";
    }
}
